package com.example.mobilesafe.interception.reciever;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;

import com.android.internal.telephony.ITelephony;
import com.example.mobilesafe.interception.db.dao.BlackNumberDao;

import java.lang.reflect.Method;

public class InterceptCallReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        BlackNumberDao dao = new BlackNumberDao(context);
        if (!intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
            String mIncomingNumber="";
            TelephonyManager tManager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
            switch (tManager.getCallState()){
                case TelephonyManager.CALL_STATE_RINGING:
                    mIncomingNumber = intent.getStringExtra("incoming_number");
                    if (mIncomingNumber == null){
                        return;
                    }
                    if (dao.isNumberExist(mIncomingNumber)){
                        Uri uri = Uri.parse("content://call_log/calls");
                        context.getContentResolver().registerContentObserver(
                                uri,
                                true,
                                new CallLogObserver(new Handler(),mIncomingNumber,
                                        context));
                        endCall(context);
                        dao.update(mIncomingNumber);
                    }
                    break;
            }
        }
    }

    public void endCall(Context context){
        try {
            Class<?> clazz = context.getClassLoader().loadClass("android.os.ServiceManager");
            Method method = clazz.getDeclaredMethod("getService",String.class);
            IBinder iBinder =(IBinder) method.invoke(null,Context.TELECOM_SERVICE);
            ITelephony itelephony = ITelephony.Stub.asInterface(iBinder);
            itelephony.endCall();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class CallLogObserver extends ContentObserver{
    private String incomingNumber;
    private Context context;
    public CallLogObserver(Handler handler,String incomingNumber,Context context){
        super(handler);
        this.incomingNumber = incomingNumber;
        this.context = context;
    }
    //观潮到数据库内容变化调用的方法

    @Override
    public void onChange(boolean selfChange) {
        context.getContentResolver().unregisterContentObserver(this);
        deleteCallLog(incomingNumber,context);
        super.onChange(selfChange);
    }
    public void deleteCallLog(String incomingNumber,Context context){
        ContentResolver resolver = context.getContentResolver();
        Uri uri = Uri.parse("content://call_log/calls");
        Cursor cursor = resolver.query(uri,new String[]{"_id"},"number=?",
                new String[]{incomingNumber},"_id desc limit 1");
        if (cursor.moveToNext()){
            String id = cursor.getString(0);
            int flag = resolver.delete(uri,"_id=?",new String[]{id});
        }
    }
}
